<template>
  <div>
    <el-dialog
      :title="title + '文件'"
      :visible.sync="dialogVisible"
      :append-to-body="true"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="120px">
          <el-form-item label="文件名" prop="oldName" v-show="addOrEdit == 2">
            <el-input v-model="form.oldName"></el-input>
          </el-form-item>
          <el-form-item label="下载权限" prop="isDownload">
            <el-radio-group v-model="form.isDownload">
              <el-radio label="1">可以</el-radio>
              <el-radio label="2">不可以</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="资源权限" prop="isDownload">
            <el-radio-group v-model="form.isPublic">
              <el-radio label="2">私有</el-radio>
              <el-radio label="1">共享</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="文件上传"
            prop="isDownload"
            v-show="addOrEdit == 1"
          >
            <Upload
              multiple
              @emit="dialogVisible = false"
              v-if="dialogVisible"
              :initData="form"
            />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  components: {
    Upload: () => import('./Upload.vue'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      form: {
        oldName: '',
        category: '1',
        pId: '0',
        classifyId: '',
        content: '',
        orderSort: '',
        isDownload: '1',
        isPublic: '2',
        showType: '',
      },
      rules: {
        oldName: [
          {
            required: true,
            message: '请输入资源名称',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  computed: mapGetters(['netPid', 'netFileSelectFlag']),
  mounted() {
    this.$bus.$on('uploadFile', this.init)
    this.$bus.$on('openFolder', (v) => {
      this.form.pId = v ? v.id : '0'
    })
  },
  beforeDestroy() {
    this.$bus.$off('uploadFile')
    this.$bus.$off('openFolder')
  },
  methods: {
    init(row) {
      if (row) {
        this.form = { ...this.form, ...row }
        this.$set(this.form, 'id', row.id)
      } else {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      }
      this.addOrEdit = row ? 2 : 1
      this.title = row ? '编辑' : '新增'
      this.dialogVisible = true
    },
    //提交
    submitForm(form) {
      if (this.addOrEdit == 1) {
        this.$bus.$emit('uploading')
      } else {
        this.$refs[form].validate((valid) => {
          if (valid) {
            this.edit()
          }
        })
      }
    },
    //修改
    async edit() {
      this.$delete(this.form, 'minAttrAddr')
      await this.$store.dispatch('updateTbNetDisk', this.form)
      this.$bus.$emit('refreshTbNetDiskList')
      this.dialogVisible = false
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
    },
  },
}
</script>